package tech.horgee.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import tech.horgee.common.IWrapper;
import tech.horgee.enums.AttendStatusEnum;
import tech.horgee.enums.AttendTypeEnum;
import tech.horgee.group.UpdateGroup;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Objects;
import java.util.Optional;

/**
 * 考勤表
 *
 * @author haojun
 * @since 2024-01-05
 */
@Getter
@Setter
@TableName("tab_attendance")
public class Attendance implements Serializable, IWrapper<Attendance> {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @NotNull(groups = UpdateGroup.class)
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 状态
     */
    private AttendStatusEnum status;

    /**
     * 异常类型
     */
    private AttendTypeEnum type;

    /**
     * 备注
     */
    private String remark;

    /**
     * 拍照
     */
    private String snapshot;

    /**
     * 经度
     */
    private String lng;

    /**
     * 纬度
     */
    private String lat;

    /**
     * 签到日期
     */
    private LocalDate signDate;

    /**
     * 签到时间
     */
    private LocalTime signTime;

    /**
     * 签退时间
     */
    private LocalTime exitTime;

    /**
     * 开始时间
     */
    @TableField(exist = false)
    private LocalDate startDate;
    /**
     * 结束时间
     */
    @TableField(exist = false)
    private LocalDate endDate;

    @TableField(exist = false)
    private String nickname;

    public String getStatusLabel() {
        return Optional.ofNullable(status).orElse(AttendStatusEnum.NORMAL).label;
    }

    public String getTypeLabel() {
        return Optional.ofNullable(type).orElse(AttendTypeEnum.NONE).label;
    }

    @Override
    public LambdaQueryWrapper<Attendance> wrapper() {
        return new LambdaQueryWrapper<Attendance>()
                .eq(Objects.nonNull(id), Attendance::getId, id)
                .eq(Objects.nonNull(userId), Attendance::getUserId, userId)
                .eq(Objects.nonNull(status), Attendance::getStatus, status)
                .eq(Objects.nonNull(type), Attendance::getType, type)
                .like(Objects.nonNull(remark), Attendance::getRemark, remark)
                .like(Objects.nonNull(snapshot), Attendance::getSnapshot, snapshot)
                .like(Objects.nonNull(lng), Attendance::getLng, lng)
                .like(Objects.nonNull(lat), Attendance::getLat, lat)
                .eq(Objects.nonNull(signDate), Attendance::getSignDate, signDate)
                .eq(Objects.nonNull(signTime), Attendance::getSignTime, signTime)
                .eq(Objects.nonNull(exitTime), Attendance::getExitTime, exitTime)
                .ge(Objects.nonNull(startDate), Attendance::getSignDate, startDate)
                .le(Objects.nonNull(endDate), Attendance::getSignDate, endDate)
                ;
    }
}
